<template>
  <div>
    <el-row :gutter="15">
      <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-col :span="15">
          <el-form-item label="文章标题" prop="article_title">
            <el-input v-model="formData.article_title" placeholder="请输入文章文章标题" clearable
                      :style="{width: '100%'}"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="文章简介" prop="des">
            <el-input v-model="formData.des" type="textarea" placeholder="请输入文章文章简介"
                      :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="排序" prop="article_sort">
            <el-input v-model="formData.article_sort" placeholder="请输入排序" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="是否展示" prop="article_swtich">
            <el-radio-group v-model="formData.article_swtich" size="medium">
              <el-radio v-for="(item, index) in article_swtichOptions" :key="index" :label="item.value"
                        :disabled="item.disabled">{{item.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="文章分类" prop="article_class">
            <el-select v-model="formData.article_class" placeholder="请选择文章分类" clearable
                       :style="{width: '100%'}">
              <el-option v-for="(item, index) in article_classOptions" :key="index" :label="item.label"
                         :value="item.value" :disabled="item.disabled"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="封面图片" prop="article_cover" required>
            <el-upload ref="article_cover" :file-list="article_coverfileList" :action="article_coverAction"
                       :before-upload="article_coverBeforeUpload" list-type="picture-card" accept="image/*">
              <i class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item size="large">
            <el-button type="primary" @click="submitForm">提交</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
  </div>
</template>
<script>
export default {
  components: {},
  props: [],
  data() {
    return {
      formData: {
        article_title: undefined,
        des: undefined,
        article_sort: undefined,
        article_swtich: 1,
        article_class: undefined,
        article_cover: null,
      },
      rules: {
        article_title: [{
          required: true,
          message: '请输入文章文章标题',
          trigger: 'blur'
        }],
        des: [{
          required: true,
          message: '请输入文章文章简介',
          trigger: 'blur'
        }],
        article_sort: [{
          required: true,
          message: '请输入排序',
          trigger: 'blur'
        }],
        article_swtich: [{
          required: true,
          message: '是否展示不能为空',
          trigger: 'change'
        }],
        article_class: [{
          required: true,
          message: '请选择文章分类',
          trigger: 'change'
        }],
      },
      article_coverAction: 'https://jsonplaceholder.typicode.com/posts/',
      article_coverfileList: [],
      article_swtichOptions: [{
        "label": "开启",
        "value": 1
      }, {
        "label": "关闭",
        "value": 2
      }],
      article_classOptions: [{
        "label": "选项一",
        "value": 1
      }, {
        "label": "选项二",
        "value": 2
      }],
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    submitForm() {
      this.$refs['elForm'].validate(valid => {
        if (!valid) return
        // TODO 提交表单
      })
    },
    resetForm() {
      this.$refs['elForm'].resetFields()
    },
    article_coverBeforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 2
      if (!isRightSize) {
        this.$message.error('文件大小超过 2MB')
      }
      let isAccept = new RegExp('image/*').test(file.type)
      if (!isAccept) {
        this.$message.error('应该选择image/*类型的文件')
      }
      return isRightSize && isAccept
    },
  }
}

</script>
<style>
.el-upload__tip {
  line-height: 1.2;
}

</style>
